layui.config({
	base : "../js/"
}).use(['flow','form','layer','upload'],function(){
    var flow = layui.flow,
        form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        upload = layui.upload,
        $ = layui.jquery;
    
    //普通图片上传
	var uploadInst = upload.render({
	    elem: '#picture',
	    size: 200,
	    url: '../fileuploadservlet.do',
	    before: function(obj){

	    },done: function(res){
	      //上传成功
	      if(res.code > 0){
	      	layer.alert('文件上传成功！',{icon:1});
	      	window.location.reload();
	      }else{
	      	layer.alert('文件上传失败！',{icon:2});
	        return ;
	      }
	      
	    }
	});

    //流加载图片
    var imgNums = 15;  //单页显示图片数量
    flow.load({
        elem: '#Images', //流加载容器
        done: function(page, next){ //加载下一页
            $.get("../sysimgmanagement/getsystemimagelist",function(res){
                //模拟插入
                var imgList = [],data = res.data;
                var maxPage = imgNums*page < data.length ? imgNums*page : data.length;
                setTimeout(function(){
                    for(var i=imgNums*(page-1); i<maxPage; i++){
                        imgList.push('<li class="liimglist"><img layer-src="/kmsximgs/photo/'+ data[i].path +'" src="/kmsximgs/photo/'+ data[i].path +'"><div class="operate"><div class="check"><span class="spanimgtitle">图片'+(i+1)+'</span></div><i delids='+data[i].photoid+' class="layui-icon img_del">&#xe640;</i></div></li>');
                    }
                    next(imgList.join(''), page < (data.length/imgNums));
                    form.render();
                }, 500);
            });
        }
    });

    //设置图片的高度
    $(window).resize(function(){
        $("#Images li img").height($("#Images li img").width());
    })

    //多图片上传
   /* upload.render({
        elem: '.uploadNewImg',
        url: '../json/userface.json',
        multiple: true,
        before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#Images').prepend('<li><img layer-src="'+ result +'" src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img"><div class="operate"><div class="check"><input type="checkbox" name="belle" lay-filter="choose" lay-skin="primary" title="'+file.name+'"></div><i class="layui-icon img_del">&#xe640;</i></div></li>')
                //设置图片的高度
                $("#Images li img").height($("#Images li img").width());
                form.render("checkbox");
            });
        },
        done: function(res){
            //上传完毕
        }
    });*/

    //点击查看图片详情
    $("body").on("click",".liimglist img",function(){
    	var img = new Image();
        img.src = $(this).attr("src");  
        var imgHtml = "<img src='" + $(this).attr("src") + "' />";
        layer.open({
            type: 1,
            shade: 0.6,
            title: false,
            area: [img.width + 'px', img.height+'px'],
            content: imgHtml,
            cancel: function () {
            }
        });
    })

    //删除单张图片
    $("body").on("click",".img_del",function(){
        var _this = $(this);
        layer.confirm('确定删除图片"'+_this.siblings().find("span").text()+'"吗？',{icon:3, title:'提示信息'},function(index){
        	var photoid=_this.attr("delids");
        	$.ajax({
    			url: '../sysimgmanagement/deleteimage',  
    	        type: 'get', 
    	        dataType: 'json',  
    	        data:{photoid:photoid},
    	        success: function (getdata) {
    	          if (getdata.code == 10001) { 
    	        	  layer.msg(getdata.msg, {icon: 1});
    	        	  _this.parents("li").hide(1000);
    	              setTimeout(function(){_this.parents("li").remove();},950);
    	              layer.close(index);
    	          }else { 
    	        	  layer.msg(getdata.msg, {icon: 2});
    	          }  
    	        },
    	        error:function(){
    	        	layer.msg('图片删除失败，请重试！', {icon: 2});
    	        }
        	});
        	
        });
    })

})

